package com.oa.user.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.oa.entity.TblPersonalInformation;
import com.oa.entity.TblRole;
import com.oa.entity.TblUser;
import com.oa.im.Biz.ImBiz;
import com.oa.role.Biz.RoleBiz;
import com.oa.user.Biz.UserBiz;
import com.oa.util.BaseAction;
import com.oa.util.MyStaticConst;

public class UserAction extends BaseAction {
	
	private TblUser tblUser;
	private TblRole tblRole;
	private TblPersonalInformation tblPi;
	private UserBiz userBiz;
	private RoleBiz roleBiz;
	private ImBiz imBiz;
	private String message;
	private JSONObject myjson;

	
	/*
	 * 用户列表
	 */
	public String user_List()throws Exception{
		String hql = "from TblUser u where 1=1";
		List ulist = this.userBiz.searchData(hql, null);
		request.getSession().setAttribute("user_list", ulist);
		return SUCCESS;
	}
	/*
	 * 用户删除
	 */
	public String user_delete()throws Exception{
		boolean flag = false;
		Integer uid = Integer.parseInt(request.getParameter("userId"));
		TblUser tu = this.userBiz.load(uid);
		if(tu!=null){
			this.userBiz.delete(tu);
			return "success";
		}else{
			System.out.println("删除失败："+uid);
			return null;
		}
	}
	/*
	 * 加载用户信息
	 */
	public String user_loader()throws Exception{
		TblUser tu = new TblUser();
		Integer uid = Integer.parseInt(request.getParameter("userId"));
		tu = this.userBiz.load(uid);
		if(tu!=null){
			request.getSession().setAttribute("user_detail", tu);
			return "success";
		}else{
			return "fail";
		}
	}
	/*
	 * 用户修改
	 */
	public String user_update(){
		String username = request.getParameter("tblUser.username");
		String password = request.getParameter("tblUser.password");
		String hql = "from TblUser tu where 1=1 and tu.username=:username";
		System.out.println();
		Map params = new HashMap();
		params.put("username", username);
		List<TblUser> ulist = this.userBiz.searchData(hql, params);
		tblUser = ulist.get(0);
		tblUser.setPassword(password);
		this.userBiz.update(tblUser);
		return null;
	}
	/**
	 * 跳转添加页面
	 * @return null
	 * @throws Exception
	 */
	public String user_to_add()throws Exception{
		String hql = "from TblRole tr where 1=1";
		List<TblRole> roles = this.roleBiz.searchData(hql, null);
		request.setAttribute("rlist", roles);
		return "success";
	}
	/**
	 * 处理添加事务
	 * @return
	 * @throws Exception
	 */
	public String user_do_add()throws Exception{
		TblUser tu = new TblUser();
		TblPersonalInformation pi = new TblPersonalInformation();
		tu.setUsername(tblUser.getUsername());
		tu.setPassword(tblUser.getPassword());
		
		pi.setFullName(tblPi.getFullName());
		int gdInt = Integer.parseInt(tblPi.getGender());
		if(gdInt==1){
			pi.setGender("男");
		}else{
			pi.setGender("女");
		}
		pi.setBirthday(tblPi.getBirthday());
		if(!(tblPi.getPhone().equals(""))&&tblPi.getPhone()!=null){
			pi.setPhone(tblPi.getPhone());
		}
		if(!(tblPi.getEmail().equals(""))&tblPi.getEmail()!=null){
			pi.setEmail(tblPi.getEmail());
		}
		pi.setCreatedate(new Date());
		tu.setTblPersonalInformation(pi);
		this.imBiz.add(pi);
		this.userBiz.add(tu);
		return SUCCESS;
	}
	
	/**
	 * 验证用户名的唯一性
	 * @return boolean
	 */
	public String user_checkLegitimacy()throws Exception{
		String username=request.getParameter("username");
		boolean flag = this.userBiz.checkLegitimacy(username);
		myjson = new JSONObject();
		if(flag){
			myjson.put("user_checkLegitimacy",0);
		}else{
			myjson.put("user_checkLegitimacy",1);
		}
		return SUCCESS;
	}
	public TblUser getTblUser() {
		return tblUser;
	}
	public void setTblUser(TblUser tblUser) {
		this.tblUser = tblUser;
	}
	public TblRole getTblRole() {
		return tblRole;
	}
	public void setTblRole(TblRole tblRole) {
		this.tblRole = tblRole;
	}
	public TblPersonalInformation getTblPi() {
		return tblPi;
	}
	public void setTblPi(TblPersonalInformation tblPi) {
		this.tblPi = tblPi;
	}
	public UserBiz getUserBiz() {
		return userBiz;
	}
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
	public RoleBiz getRoleBiz() {
		return roleBiz;
	}
	public void setRoleBiz(RoleBiz roleBiz) {
		this.roleBiz = roleBiz;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public JSONObject getMyjson() {
		return myjson;
	}
	public void setMyjson(JSONObject myjson) {
		this.myjson = myjson;
	}
	public ImBiz getImBiz() {
		return imBiz;
	}
	public void setImBiz(ImBiz imBiz) {
		this.imBiz = imBiz;
	}
}
